package com.df.bpm.utils.businessViewerV2.business;

import com.df.bpm.utils.businessViewerV2.Main;
import com.df.bpm.utils.businessViewerV2.informatization.M_outputDTO;
import com.df.common.utils.CommonUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.List;

public class V_businessByOutput {

    public static int unit=10;
    public JDialog jd=new JDialog();
    public JLayeredPane jp=new JLayeredPane();
    Font iFont;

    M_outputDTO goldenDto;

    Integer goldenRequirement;

    V_scroll4BusinessDto vScroll4BusinessDto;
    V_scroll4SuperiorBusinessDto vSuperior;
    V_scroll4HistoryBusinessDto vHistory;

    V_scroll4BusinessGroup vGroup;

    V_scroll4BusinessSteps vSteps;

    V_scroll4InputDto vInput;

    V_scroll4OutputDto vOutput;

    Integer activeBusiness,activeGroup;

    JLabel activeBusinessDesc=new JLabel();

    JButton freshInputOutput=new JButton("刷新");

    /**
     * 输出物相关业务浏览面板
     * @param requirementId 需求id
     * @param outputDTO 输出物dto
     */
    public V_businessByOutput(Integer requirementId, M_outputDTO outputDTO) {
        goldenRequirement=requirementId;
        goldenDto=outputDTO;
        unit= (int) Main.unit;
        iFont=new Font("宋体", Font.PLAIN, unit);
        jd.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                // 关闭窗口时触发
            }
        });
        jd.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        jd.setSize(Main.maxSize);
        jd.setTitle(outputDTO.getEntityName()+"_相关业务信息");
    }

    public void repaint() {
        jd.remove(jp);
        jd.setLocationRelativeTo(null);
        jd.setLayout(null);
        jp=new JLayeredPane();
        jp.setLayout(null);
        jp.setSize(jd.getSize());

        int left_scroll_height=(jp.getHeight()-4*unit);

        vScroll4BusinessDto=new V_scroll4BusinessDto(goldenRequirement,goldenDto.getBusinessEntityIndex(),this);
        vScroll4BusinessDto.setSize(20*unit,left_scroll_height/3);
        vScroll4BusinessDto.setLocation(0, 0);
        jp.add(vScroll4BusinessDto);

        vSuperior=new V_scroll4SuperiorBusinessDto(this);
        vSuperior.setSize(vScroll4BusinessDto.getWidth(),left_scroll_height/3);
        vSuperior.setLocation(vScroll4BusinessDto.getX(), vScroll4BusinessDto.getY()+vScroll4BusinessDto.getHeight());
        jp.add(vSuperior);

        vHistory=new V_scroll4HistoryBusinessDto(this);
        vHistory.setSize(vScroll4BusinessDto.getWidth(),left_scroll_height/3);
        vHistory.setLocation(vScroll4BusinessDto.getX(), vSuperior.getY()+vSuperior.getHeight());
        jp.add(vHistory);
        vHistory.putModel(null);

        vGroup=new V_scroll4BusinessGroup(this);
        vGroup.setSize(4*unit, unit*5);
        vGroup.setLocation(vScroll4BusinessDto.getX()+vScroll4BusinessDto.getWidth(), vScroll4BusinessDto.getY());
        jp.add(vGroup);

        freshInputOutput.setSize(4*unit,unit*2);

        activeBusinessDesc.setSize(jp.getWidth()-vScroll4BusinessDto.getWidth()-vGroup.getWidth()-freshInputOutput.getWidth()-4*unit,
                unit*5);
        activeBusinessDesc.setLocation(vGroup.getX()+vGroup.getWidth(), vGroup.getY());
        activeBusinessDesc.setFont(new Font("宋体", 0, unit*4));
        activeBusinessDesc.setHorizontalAlignment(SwingConstants.LEFT);
        jp.add(activeBusinessDesc);

        freshInputOutput.setLocation(activeBusinessDesc.getX()+activeBusinessDesc.getWidth(),
                activeBusinessDesc.getY()+activeBusinessDesc.getHeight()-freshInputOutput.getHeight());
        jp.add(freshInputOutput);
        freshInputOutput.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if(activeBusiness!=null){
                    C_business.freshInputOutput(activeBusiness);
                    setActiveGroup(activeGroup);
                    JOptionPane.showMessageDialog(jp,"刷新完毕", "注意",JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });

        int outputWidth=22*unit;

        vSteps= new V_scroll4BusinessSteps(this);
        vSteps.setSize(jp.getWidth()-vScroll4BusinessDto.getWidth()-4*unit-outputWidth,
                left_scroll_height-vGroup.getHeight());
        vSteps.setLocation(vGroup.getX(), activeBusinessDesc.getY()+activeBusinessDesc.getHeight());
        jp.add(vSteps);

        vInput= new V_scroll4InputDto(this);
        vInput.setSize(outputWidth,vSteps.getHeight()/2);
        vInput.setLocation(vSteps.getX()+vSteps.getWidth(), vSteps.getY());
        jp.add(vInput);

        vOutput= new V_scroll4OutputDto(this);
        vOutput.setSize(outputWidth,vSteps.getHeight()/2);
        vOutput.setLocation(vInput.getX(), vInput.getY()+vInput.getHeight());
        jp.add(vOutput);

        jd.add(jp);

        M_businessDto temp=vScroll4BusinessDto.getModel().get(0);
        if(temp.getBusinessInformationIndex()!=null){
            this.setActiveBusiness(temp.getBusinessInformationIndex(),temp.getBusinessDesc());
            vHistory.insert(temp.getBusinessInformationIndex(),temp.getBusinessDesc());
        }
        jd.setModal(true);
        jd.repaint();

    }

    public void setVisible(boolean isVisible) {
        jd.setVisible(isVisible);
    }

    /**
     * 展示当前分组
     * @param group 当前分组
     */
    public void setActiveGroup(Integer group) {
        activeGroup=group;
        List<M_businessLocationsDTO4Subset> locationDto4Subsets = C_business.getLocationDto4Subsets(activeBusiness, activeGroup);
        vSteps.putModel(locationDto4Subsets);// 按首选分组查步骤流程
        String iTitle="业务流程";
        if(group!=null){
            iTitle="分组"+group+"的业务流程";
        }
        vSteps.setBorder(BorderFactory.createTitledBorder(iTitle));
    }

    /**
     * 展示当前业务
     * @param businessIndex 当前业务id
     * @param businessDesc  当前业务简述
     */
    public void setActiveBusiness(Integer businessIndex,String businessDesc) {
        this.activeBusiness=businessIndex;
        this.activeBusinessDesc.setText(businessDesc);
        List<Integer> groupIds = C_business.getGroupIds(activeBusiness);
        vGroup.putModel(groupIds);
        activeGroup=null;
        if(CommonUtils.varIsNotBlank(groupIds)){
            activeGroup=groupIds.get(0);
            setActiveGroup(activeGroup);
        }else{
            vSteps.putModel(null);
        }
        showBusinessInputOutput(activeBusiness);
        List<M_businessLocationsDTO4Subset> superiors=C_business.getSuperiorDtos(activeBusiness);
        vSuperior.putModel(superiors);// 查询父级业务

    }

    /**
     * 展示目标业务的输入物和输出物
     * @param businessId 目标业务id
     */
    public void showBusinessInputOutput(Integer businessId){
        vInput.showBusiness(businessId);
        vOutput.showBusiness(businessId);

    }

}
